<?php
$auth = Auth::getAuth('current_user');
$db = new Database();
$fs = new functions();
$userCompany = new userQueries();

$timezone = "Asia/Manila";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);

    if(isset($_POST['action'])){
        $action = $_POST['action'];
        $date = $fs->currentDateTime();
        
            if($action=="loadFormCategory"){
                $res = "";
                    $out = "";
                    $fields_out = "";
                    $company_user_info = array();
                    $fields = array(); 
                    
                    //$companyUsers_fields = $db->query("SHOW COLUMNS FROM tbuser");
                    $company = $userCompany->getCompany($auth['company_id']);
                    $company_id = $auth['company_id'];
                    $suggestion_sql = $db->query("SELECT * FROM  `tbform_category` WHERE company_id = {$db->escape($company_id)} AND is_active = 1 ORDER BY id DESC","array");
                    // Table fields
                        //foreach($companyUsers as $info){
                        //    $company_user_info[] = $info;
                        //}
                        
                        
                        foreach($suggestion_sql as $info){
                            // Actions
                            $actions = '<center><i class="icon-trash fa fa-trash-o tip cursor deleteformCategory" data-placement="top" data-original-title="Delete" style="margin-right:5px" data-id="'. $info['id'] .'"></i> <i class="icon-edit fa fa-pencil-square-o tip cursor updateformCategory" data-placement="top" data-original-title="Edit" data-id="'. $info['id'] .'" data-name="'. $info['category_name'] .'"></i><div class="display users_'. $info['id'] .'">'. $info['users'] .'</div><div class="display image_'. $info['id'] .'">'. $info['image'] .'</div></center>';
                              
                            $company_user_info[] = array("id"=>$info['id'],
                                                        "title"=>$info['category_name'],
                                                        "actions"=>$actions);
                            
                        }
                            $res = array(/*"sEcho"                        =>      "3",
                                         "iTotalRecords"                =>      $countRows,
                                         "iTotalDisplayRecords"         =>      $countRows,*/
                                         "company_user"                 =>      $company_user_info);
                        
                            $string = json_encode($res);
                            
                            echo $string;
            }else if($action=="loadFormCategoryDatatable"){
                $search_value = $_POST['search_value'];
                $start = $_POST['iDisplayStart'];
                $limit = "";
                if($start!=""){
                    $limit = " LIMIT $start, 10";
                } 
                $company_id = $auth['company_id'];
                $orderBy = " ORDER BY id DESC";
                if($_POST['column-sort']){
                  $orderBy = " ORDER BY ".$_POST['column-sort']." ".$_POST['column-sort-type'];
                }
                $query = "SELECT * FROM  `tbform_category` WHERE company_id = {$db->escape($company_id)} AND is_active = 1 AND category_name LIKE '%". $search_value ."%' $orderBy ";
                // echo $orderBy;
                $suggestion_sql = $db->query($query.$limit,"array");
                $countForm = $db->query($query,"numrows");
                
                
                $output = array(
                    "sEcho" => intval($_POST['sEcho']),
                    "iTotalRecords" => $countForm,
                    "iTotalDisplayRecords" => $countForm,
                    "start"=>$start,
                    "aaData" => array(),
                );    
                    foreach($suggestion_sql as $info){
                        // Actions
                        $company_user_info = array();  
                        $actions = '<center><i class="icon-trash fa fa-trash-o tip cursor deleteformCategory" data-placement="top" data-original-title="Delete" style="margin-right:5px" data-id="'. $info['id'] .'"></i> <i class="icon-edit fa fa-pencil-square-o tip cursor updateformCategory" data-placement="top" data-original-title="Edit" data-id="'. $info['id'] .'" data-name="'. $info['category_name'] .'"></i><div class="display users_'. $info['id'] .'">'. $info['users'] .'</div><div class="display image_'. $info['id'] .'">'. $info['image'] .'</div></center>';
                          
                        $company_user_info[] = "<div class=''>" . $info['category_name']."</div>";
                        $company_user_info[] = $actions;
                        $output['aaData'][] = $company_user_info;
                        
                    }
                    
                    
                    echo json_encode($output);
                    // echo $query.$limit;
            }else if($action=="deleteformCategory"){
                $id = $_POST['id'];
                $strSql = "SELECT id,form_json FROM tb_workspace WHERE company_id = {$db->escape($auth[company_id])}";
                $getForms = $db->query($strSql,"array");
                $json = array();
                foreach ($getForms as $forms) {
                    $json_arr =  json_decode($forms['form_json'],true);
                    if($id == $json_arr['categoryName']){
                        array_push($json, $forms);
                    }
                }
                echo json_encode($json);
            }else if($action=="updateForms"){
                $formToUpdate = $_POST['formToUpdate'];
                $id = $_POST['id'];
                foreach ($formToUpdate as $value) {
                    $update = array("form_json"=>$value['form_json']);
                    $where = array("id"=>$value['id']);
                    $db->update("tb_workspace",$update,$where);
                }
                $where_fc = array("id"=>$id);
                $db->delete("tbform_category",$where_fc);
            }else if($action=="addCategory"){
                $category_name = $_POST['category_name'];
                $json_privacy = $_POST['users'];
                $insert = array("category_name"=>$category_name,
                                "company_id"=>$auth['company_id'],
                                "users"=>$json_privacy,
                                "image"=>$_POST['logo'],
                                "is_active"=>1);
                $db->insert("tbform_category",$insert);
            }else if($action=="editCategory"){
                $category_name = $_POST['category_name'];
                $category_id = $_POST['category_id'];
                $json_privacy = $_POST['users'];
                $update = array("category_name"=>$category_name,
                                "users"=>$json_privacy,
                                "image"=>$_POST['logo']);
                $where = array("id"=>$category_id);
                $db->update("tbform_category",$update,$where);
            }
    }


?>